fix(react-form): use shallow equality in form.Subscribe to prevent unnecessary re-renders#2092
Conversation
…necessary re-renders When the selector returns an object or array, useStore falls back to Object.is reference equality, causing re-renders on every store update even when the selected values haven't changed. Pass the shallow equality function from @tanstack/react-store as the compare argument to useStore so Subscribe only re-renders when the selected data actually changes structurally. Fixes TanStack#2090
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughUpdated the internal subscription mechanism in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
View your CI Pipeline Execution ↗ for commit cc25192
☁️ Nx Cloud last updated this comment at |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2092 +/- ##
===========================================
- Coverage 90.35% 56.68% -33.68%
===========================================
Files 38 18 -20
Lines 1752 247 -1505
Branches 444 47 -397
===========================================
- Hits 1583 140 -1443
+ Misses 149 92 -57
+ Partials 20 15 -5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
Fixes #2090
form.Subscribewas triggering re-renders on every store update when theselectorreturned an object or array. SinceuseStoredefaults toObject.isreference equality, selectors that construct new objects/arrays on each call always appeared to have changed — even when the underlying data was identical.Fix
Pass
shallowfrom@tanstack/react-storeas the compare function touseStoreinLocalSubscribe. This is a one-line change: shallow equality avoids re-renders when the selector returns an object or array with the same primitive values.@tanstack/react-storealready exportsshallow(and re-exports@tanstack/store), so no new dependencies are needed.Testing
All 86 existing tests pass with no changes. The fix is intentionally minimal — the change in equality semantics only matters when a selector returns a new object/array reference on every call with the same contents, which is the exact scenario described in the issue.
Summary by CodeRabbit